package cn.lanqiao.book.controller;

import cn.lanqiao.book.dao.IBookDao;
import cn.lanqiao.book.dao.ICategoryDao;
import cn.lanqiao.book.dao.IChapterDao;
import cn.lanqiao.book.dao.impl.BookDaoImplI;
import cn.lanqiao.book.dao.impl.CategoryDaoImplI;
import cn.lanqiao.book.dao.impl.ChapterDaoImplI;
import cn.lanqiao.book.pojo.Book;
import cn.lanqiao.book.pojo.category;
import cn.lanqiao.book.pojo.chapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class categoryServlet extends HttpServlet {

    ICategoryDao cd = new CategoryDaoImplI();
    category c = new category();
    Book bo = new Book();
    IBookDao bd = new BookDaoImplI();
    IChapterDao chd = new ChapterDaoImplI();
    chapter ch = new chapter();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("_method");
        try {
            Method m = getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void addCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            out.println("未登录");
            resp.sendRedirect("/login.jsp");
        } else {
            int type1 = (int) type;
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("c_name");
                c.setC_name(name);
                cd.addCatefory(c);
            }
        }
        req.getRequestDispatcher("/category.jsp").forward(req, resp);
    }

    public void deleteCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();

        Object type = session.getAttribute("type");
        if (type == null) {
            out.println("未登录");
            resp.sendRedirect("/login.jsp");
        } else {
            int type1 = (int) type;
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("c_name");
                c = cd.queryForOne(name);
                int cid = c.getC_id();
                List<Book> l = new ArrayList<>();
                l = bd.searchbyid(cid);
                for (Book book : l) {
                    String bname = book.getB_name();
                    int bid = book.getB_id();
                    chd.deleteChaptervyid(bid);
                    bd.deleteBook(bname);
                }


                cd.deleteCategory(name);
            } else {
                out.println("权限不足");
            }
        }
    }

    public void selectCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");

        if (type == null) {
            out.println("未登录");
        } else {
            int type1 = (int) type;
            if (type1 == 2 || type1 == 3) {
                List<category> l = new ArrayList<>();
                l = cd.queryAll();
                req.setAttribute("category", l);
                req.getRequestDispatcher("/selectCategory.jsp").forward(req, resp);
            } else {
                out.println("权限不足");
            }
        }
    }

    public void qurryCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Object type = session.getAttribute("type");
        if (type == null) {
            out.println("未登录");
            resp.sendRedirect("/login.jsp");
        } else {
            int type1 = (int) type;
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("c_name");
                c = cd.queryForOne(name);
                req.setAttribute("category", c);
                req.getRequestDispatcher("/update2.jsp").forward(req, resp);
            } else {
                out.println("权限不足");
            }
        }
    }

    public void updateCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("c_name");
        int id = Integer.parseInt(req.getParameter("c_id"));
        c.setC_id(id);
        c.setC_name(name);
        cd.updateCategory(c);
        req.getRequestDispatcher("/category.jsp").forward(req, resp);
    }

    public void searchCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("c_name");
        name = "%" + name + "%";
        List<category> l = new ArrayList<>();
        l = cd.search(name);
        req.setAttribute("category3", l);
        req.getRequestDispatcher("/search3.jsp").forward(req, resp);
    }

}
